package ca.etsmtl.log430.lab2.presentation.wingui;

import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import java.util.List;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import ca.etsmtl.log430.lab2.gestion.Course;
import ca.etsmtl.log430.lab2.gestion.Teacher;
import ca.etsmtl.log430.lab2.gestion.TeacherAssignmentException;
import ca.etsmtl.log430.lab2.gestion.ifaces.ICourse;
import ca.etsmtl.log430.lab2.gestion.ifaces.ITeacher;
import ca.etsmtl.log430.lab2.gestion.ifaces.ITeacherAssignment;
import ca.etsmtl.log430.lab2.presentation.IDisplay;

public class JFrameDisplay extends JFrame implements IDisplay , ActionListener, ListSelectionListener
{
	private ITeacherAssignment controller;
	
	Vector<Teacher> teacherVector;
	Vector<ICourse> courseVector;
	JList<ICourse> courseList;
	JList<Teacher> teacherList;
	JList<ICourse> selectedTeacherCourseList;
	JButton assingButton;

	public JFrameDisplay(ITeacherAssignment controller)
			throws HeadlessException {
		super();
		this.controller = controller;
		this.setSize(500,500);
		
		teacherList = new JList<Teacher>();
		courseList = new JList<ICourse>();
		selectedTeacherCourseList = new JList<ICourse>();
		
		teacherVector = new Vector<Teacher> ();
		courseVector = new Vector<ICourse>();
		
		
		assingButton = new JButton("Assign Course");
		
		assingButton.addActionListener(this);
		assingButton.setActionCommand("ASSIGN_COURSE");
		
		for (Teacher iterable_element : controller.getListOfTeachers()) {
			teacherVector.add(iterable_element);
		}
		
		for (Course iterable_element : controller.getListOfCourses()) {
			courseVector.add(iterable_element);
		}
		
		
		teacherList.setListData(teacherVector);
		courseList.setListData(courseVector);
		this.setLayout(new GridLayout(2, 2));
		JScrollPane pane1 = new JScrollPane(teacherList);
		JScrollPane pane2 = new JScrollPane(courseList);
		this.add(pane1);
		this.add(pane2);
		this.add(assingButton);
		this.add(selectedTeacherCourseList);
		this.setVisible(true);
		teacherList.addListSelectionListener(this);
		
	}

	@Override
	public void displayTeacher(ITeacher teacher) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void displayCourse(ICourse course) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void displayTeachersAssignedToCourse(ICourse course) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void displayCoursesAssignedToTeacher(ITeacher teacher) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void displayTeacherList(Collection<? extends ITeacher> list) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void displayCourseList(Collection<? extends ICourse> list) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void displayException(TeacherAssignmentException e) {
		JOptionPane.showMessageDialog(null, e.getMessage(), "Error",  JOptionPane.ERROR_MESSAGE);
		
	}

	@Override
	public void displaySigleList(Collection<String> courses) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("ASSIGN_COURSE"))
		{
			List<ICourse> selectedCourses = courseList.getSelectedValuesList();
			Teacher teacher = teacherList.getSelectedValue();
			for (ICourse course : selectedCourses) {
				try 
				{
					controller.assignTeacherToCourse(teacher, course);
				} 
				catch (TeacherAssignmentException e1) {
					this.displayException(e1);
				}
			}
			
			
		}
		
	}

	@Override
	public void valueChanged(ListSelectionEvent arg0) {
		Teacher teacher = teacherList.getSelectedValue();
		Vector<ICourse> assignedCourse = new Vector<ICourse>();
		for (ICourse iterable_element : teacher.getCoursesAssigned()) {
			
			assignedCourse.add(iterable_element);

		}
		
		selectedTeacherCourseList.setListData(assignedCourse);
		
	}

}
